from asyncio.windows_events import NULL
from flask import Flask, abort,jsonify, make_response, redirect, render_template, request
import json
app = Flask(__name__)

@app.route('/' , methods=['GET','POST'])
def index():
    if request.method == 'GET':
        return render_template("index.html")
    if request.method == 'POST':
        name = request.form.get("name")
        password = request.form.get("password")
        data = {
            "username":name,
            "password":password
        }
        return jsonify(data)
@app.route("/user/<int:id>")
def getUser(id):
    return "你拿到了 + " + str(id) 

@app.route("/redirecttest")
def redire():
    return redirect("https://www.notpnzy.cn")


'''
    主动抛出异常
'''
@app.route("/exce")
def exce():
    abort(404)
    return NULL

'''
    自定义错误处理方法
'''
@app.errorhandler(404)
def handle_404_err(err):
    return render_template("404.html")

'''
    静态传值
'''
@app.route("/resdata")
def resdata():
    data = {
        "user":'frank',
        "age":18,
        'mylist' : [1,2,3,4,5,6]
    }
    
    return render_template("res.html",data = data)

'''
    自定义过滤器
'''
li = [1,2,3,4,5]
def list_step(li):
     return li[::2]

# 注册过滤器 第一个写函数名字，第二个写你要用的名字
app.add_template_filter(list_step,'li2')
if __name__ == '__main__':    app.run(host='127.0.0.1',port=5000,debug = True)